from io import FileIO
from typing import Text
from pprint import pprint
import chardet
import json
#文件结构
class Vpd:
    class VPDBone:
        boneName:str
        translater="vec3"
        quat="quaternion"
    class VPDMorph:
        morph_name:str
        weight:float
    class VPDFile:
        bones="vector"
        morphs="vector"
    pass
def parse(file:FileIO):
    vpd={}
    def read_name(file):
        return file.readline().strip().decode()
    def read_model_name(file):
        file.readline()
        name:str=file.readline().decode("shift-jis").split(";")[0]
        return name
    def get_bones(file):
        bones=file.readline().decode().split(";")[0]
        print("骨骼数量"+bones)
        return int(bones)
    def read_pose(file):
        poses=[]
        head=""
        #对每个数据块解析
        def parse_chunk():
            each_pose={}
            #第一行
            line_head=file.readline().decode("shift-jis").strip()
            #bone0{} {}之前的骨骼索引
            if "{" in line_head:
                #改用split方法
                bone_id=line_head.split("{")[0]
                bone_name=line_head.split("{")[1]
                each_pose['bone_id']=bone_id
                each_pose['bone_name']=bone_name
            line2=file.readline().decode().strip()
            #位置
            translation=line2.split(";")[0]
            line3=file.readline().decode().strip()
            #四元数
            quat=line3.split(";")[0]
            each_pose['translation']=translation
            each_pose['quat']=quat
            #{ 第四行
            line4=file.readline().decode().strip()
            if "}" in line4:
                file.readline()
            return each_pose
         #遍历
        for each in range(vpd['bones']):
            print(each)
            each_chunk_result=parse_chunk()
            poses.append(each_chunk_result)
        print("结果数据******")
        pprint(poses)
        return poses
    vpd['header']=read_name(file)
    vpd['model_name']=read_model_name(file)
    vpd['bones']=get_bones(file)
    file.readline()
    vpd['pose']=read_pose(file)
    return vpd